//
//  MLNetworkTools.swift
//  SinaWeibo
//
//  Created by 李亮亮 on 16/4/9.
//  Copyright © 2016年 Big Fish. All rights reserved.
//

import UIKit
import AFNetworking


//使用结构体来定义接口名称
struct API {
    //线上环境
    static let onlineHost = "https://api.weibo.com/"
    //测试环境  在上传AppStore之前 一定要取消使用这个环境
    static let devHost = "https://dev.api.weibo.com/"
    static let authrizon = "oauth2/access_token"
    static let access_token = "2/users/show.json"
    static let homepage = "2/statuses/home_timeline.json"
}

//swift 中的枚举
enum MLHttpMethods: String {
    case POST = "POST"
    case GET = "GET"
}

class MLNetworkTools: AFHTTPSessionManager {
    //声明单例对象
    static let sharedTools: MLNetworkTools = {
        let tools = MLNetworkTools(baseURL: NSURL(string: API.onlineHost))
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
}


// MARK: - 登录和注册相关
extension MLNetworkTools {
    // MARK: 加载用户token
    func loadAccessToken(code: String, finished: (result: AnyObject?, error: NSError?) -> ()) {
        // ANF中参数 以字典的形式传递
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type": "authorization_code","code":code,"redirect_uri":redirect_uri]
        // 调用核心方法
        request(.POST, urlString: API.authrizon, parameters: parameters, finished: finished)
    }
    
    // MARK: 加载用户信息
    func loadUserInfo(token: AnyObject, uid: AnyObject, finished:(result: AnyObject?, error: NSError?) -> ()) {
        // 字典数组中用户信息不能为nil
        let parameters = ["access_token": token,"uid": uid]
        request(.GET, urlString: API.access_token, parameters: parameters, finished: finished)
    }
    
}


// MARK: - 首页数据相关
extension MLNetworkTools {
    func loadHomePageData(token: String, max_id: Int64,since_id: Int64, finished: (result: AnyObject?, error: NSError?) -> ()) {
        var parameters = ["access_token": token]
        if max_id > 0 {
            parameters["max_id"] = "\(max_id - 1)"
        }
        if since_id > 0 {
            parameters["since_id"] = "\(since_id)"
        }
        
        request(.GET, urlString: API.homepage, parameters: parameters, finished: finished)
    }
}


// MARK: - 网络请求的核心方法 以后所有的get 和 post 都走这个方法
extension MLNetworkTools {
    //封装所有的网络请求方法 所有的网络请求都是通过这个方法和 AFN进行联系
    func request(method: MLHttpMethods,urlString: String, parameters: AnyObject?, finished: (result: AnyObject?, error: NSError?) -> ()) {
        
        //swift中就不能够使用利用协议 欺骗Xcode
        //为了达到封装的目的 可以将相同的闭包 抽取出来,当做参数传递给AFN框架
        let success =  { (task: NSURLSessionDataTask, result: AnyObject?) -> Void in
            //执行成功的回调
            finished(result: result, error: nil)
        }
        
        //定义失败的回调
        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
            //执行失败的回调
            finished(result: nil, error: error)
        }
        
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else {
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
    }

}

    

